/*
Copyright (C) 2003 Michael Würtinger

This file is part of Dynamicengine.

Dynamicengine is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.

Dynamicengine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Dynamicengine. If not, see http://www.gnu.org/licenses/.
*/


#ifndef PHYSICS_H
#define PHYSICS_H

#include "material.h"
#include "include.h"


#define MAX_TRUSSES	100
#define XSIZE		7.0
#define YSIZE		5.0
#define DISTANCE	1.0


#define PI 3.141592654


class CTruss;
class CJoint;
class CSite;

class CSite
{
public:
	CSite();
	~CSite();
public:
	list<CTruss*> trusses;
	list<CJoint*> joints;
	list<CJoint*> static_joints;
	vec3f gravity;
	float friction;
	float ground_friction;

public:
	CTruss* CreateTruss(CJoint* join1, CJoint* join2, CMaterial* material);
	CJoint* CreateJoint(bool bStatic, vec3f pos, vec3f velocity = vec3f(0.0, 0.0, 0.0), float friction = 0.5);
	void Clear();
	void RemoveTruss(CTruss* pTruss);
	void RemoveJoint(CJoint* pJoint);
	CJoint* IsJoint(float x, float y);
	void Simulate(double t);
	void Draw();
};


class CTruss
{
public:
	CTruss(CSite* Site, CJoint* joint1, CJoint* joint2, CMaterial* material);
	~CTruss();
public:
	//bool RemoveFromJoint(CJoint* joint);
	float* color();
	bool Break();
public:
	CSite* pSite;
	CJoint* joints[2];
	float length;
	float d;
	float max_pull;
	float max_pressure;
	vec3f F;
	float m;
	float stress;
};


class CJoint
{
public:
	CJoint(CSite* Site, vec3f pos, vec3f velocity = vec3f(0.0, 0.0, 0.0), float _friction = 0.5);
	~CJoint();
public:
	bool AddTruss(CTruss* truss);
	bool RemoveTruss(CTruss* truss);
public:
	CSite* pSite;
	CTruss* trusses[MAX_TRUSSES];
	float m;
	vec3f r;
	vec3f v;
	float friction;
};


#endif
